package com.xiaoy.core.bpm.simple.service;

import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.PropertyFilter;

import com.google.common.collect.Lists;
import com.xiaoy.core.bpm.engine.entity.ProcessDefineVO;
import com.xiaoy.core.bpm.engine.entity.ProcessNodeVO;
import com.xiaoy.core.bpm.simple.dao.ProcessDefineDao;
import com.xiaoy.core.bpm.simple.entity.ProcessDefine;

/**
 * 流程的定义
 * 	部署流程
 *  修改流程
 *   暂停
 *   启用
 *  删除流程
 * @author chentianyi
 */
@Service("processDefineService")
public class ProcessDefineService extends IProcessDefineAdapt{

	
//	private DaoUtil dao;
	@Resource
	private ProcessDefineDao processDefineDao;
	
	
	@Transactional
	public ProcessDefine deploymentProcess (ProcessDefine info,InputStream inputStream,String inputStreamName) {
		
//		System.out.println(info.getName());
		ProcessDefineVO define = workFlow.defineProcess(inputStreamName,inputStream);
		info.setProcessDefineName(define.getName());
		info.setProcessDefineId(define.getId());
		info.setProcessDeploymentId(define.getDeploymentId());
		info.setProcessDefineKey(define.getKey());
		
		processDefineDao.save(info);
		
		return info;
	}
	
	
	@Override
	public List<ProcessDefine> deploymentProcessList() {
		
//		Struts2Utils.getRequest().setAttribute("definitions", workFlow.getAllProcessDefinitions());
		
		return processDefineDao.find();
//		return workFlow.getAllProcessDefinitions();
	}
	
	public ProcessDefine getProcessDefineById (long key) {
		
//		processDefineDao.findUniqueBy(propertyName, value)
//		
		List<PropertyFilter>f=Lists.newArrayList();
		f.add(new PropertyFilter());
		processDefineDao.find(f);
		
		List<ProcessDefine> list = processDefineDao.findBy("id", key);
		if (list.size()>0) {
			return list.get(0);
		}
		return null;
	}

	public ProcessDefine getProcessDefineBy (String key,Object value) {
		
		List<ProcessDefine> list = processDefineDao.findBy(key, value);
		if (list.size()>0) {
			return list.get(0);
		}
		return null;
	}
	
//	public List<ProcessNodeVO> getProcessNodeByDefineId(String defineId) {
//		
//		return workFlow.getProcessNodesByDefineId(defineId);
//	}
	
	public List<ProcessDefine> getAll () {
		return processDefineDao.getAll();
	}
	
	
	
}
